#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time
from mie.bprint import cp

### ###########################################################
## Decorator print time cost of one function
#


class timetan(object):
    def __init__(self, banner=None):
        self.banner = banner or "Unknown"

    def __call__(self, fn):
        def timefn(*args, **kwargs):
            start = time.time()
            result = fn(*args, **kwargs)
            end = time.time()
            logmsg = "%s: %f" % (cp.r("COST: %s" % self.banner), end - start)
            print(logmsg)
            return result

        return timefn
